package cn.itcast.bos.web.action.system;

import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.service.system.RoleService;
import cn.itcast.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: SOS
 * @create: 2018-03-12 22:19
 */

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role> {

    @Autowired
    private RoleService roleService;

    /**
     * 查询所有的role
     * @return
     */
    @Action(value = "role_list", results = {@Result(name = SUCCESS,type = "json"),
            @Result(name = "input", location = "./pages/system/role.html", type = "redirect")})
    public String roleFindAll(){

        List<Role> roles = roleService.findAll();
        ActionContext.getContext().getValueStack().push(roles);
        return SUCCESS;
    }

    /**
     * 属性驱动接受permissionids和menuids
     *
     */
    private String[] permissionIds;

    private String menuIds;

    public void setPermissionIds(String[] permissionIds) {
        this.permissionIds = permissionIds;
    }

    public void setMenuIds(String menuIds) {
        this.menuIds = menuIds;
    }

    /**
     * 保存role的代码
     * @return
     */
    @Action(value = "role_save", results = {@Result(name = SUCCESS, location = "./pages/system/role.html", type = "redirect"),
            @Result(name = "input", location = "./pages/system/role.html", type = "redirect")})

    public String roleSave(){
         roleService.roleSave(model,permissionIds,menuIds);
        return SUCCESS;
    }
}
